(function ($) {

    /*$.fn.extend({
        runUp: function (options) {

            var ReltargetDate = $.extend({}, $.fn.runUp.defaults, options.targetDate);
            // console.log(ReltargetDate);
            var targetTime = new Date(ReltargetDate.month + '/' + ReltargetDate.day + '/' + ReltargetDate.year + ' ' + ReltargetDate.hour + ':' + ReltargetDate.min + ':' + ReltargetDate.sec + (ReltargetDate.utc ? ' UTC' : ''));

            var nowTime = new Date();

            var timestamp = Math.floor((nowTime.valueOf() - targetTime.valueOf()) / 1000);

            var secs = timestamp % 60;
            var mins = Math.floor(timestamp / 60) % 60;
            var hours = Math.floor(timestamp / 60 / 60) % 24;
            var mons = Math.floor(timestamp / 60 / 60 / 24 / 30) % 12;
            var days = Math.floor(timestamp / 60 / 60 / 24) % 365 % 30;
            var years = Math.floor(timestamp / 60 / 60 / 24 / 365);

            // console.log(years + '-' + mons + '-' + days + ' ' + hours + ':' + mins + ':' + secs);

            var time_arr = [secs, mins, hours, days, mons, years];

            return $(this).timeUp(time_arr);
        },

        runUp.defaults: {sec: 0, min: 0, hour: 0, day: 1, month: 1, year: 2000};//默认值

        timeUp: function (time_arr) {

            // console.log($(this));
            // console.log($(this).attr('id'));
            var timeUp_dom = $(this).attr('id');

            /!*for (var i = 0; i < time_arr.length; i++) {
                $('#' + timeUp_dom).append('<span><i></i></span>');
            }*!/

            //运行时间初始化
            var count = time_arr.length;
            var New_time_arr = [];
            for (var key in time_arr) {
                var ph_val = (time_arr[key] == '') ? 0 : time_arr[key] * (-44);
                New_time_arr[key] = ph_val;
                $('#' + timeUp_dom + ' span:nth-child(' + count + ') i').css({"background-position": '0 ' + ph_val + 'px'});
                count--;
            }

            var sec = New_time_arr[0];
            var min = New_time_arr[1];
            var hour = New_time_arr[2];
            var day = New_time_arr[3];
            var mon = New_time_arr[4];
            var year = New_time_arr[5];

            setInterval(function () {
                sec = (sec == -2596) ? 0 : (sec - 44);
                t_item(timeUp_dom, 6, sec);
                if (sec == 0) {
                    min = (min == -2596) ? 0 : (min - 44);
                    t_item(timeUp_dom, 5, min);
                    if (min == 0) {
                        hour = (hour == -1012) ? 0 : (hour - 44);
                        t_item(timeUp_dom, 4, hour);
                        if (hour == 0) {
                            day = (day == -1276) ? 0 : (day - 44);
                            t_item(timeUp_dom, 3, day);
                            if (day == 0) {
                                mon = (mon == -484) ? 0 : (mon - 44);
                                t_item(timeUp_dom, 2, mon);
                                if (mon == 0) {
                                    year = year - 44;
                                    t_item(timeUp_dom, 1, year);
                                }
                            }
                        }
                    }
                }
            }, 1000);
        },

        t_item: function (dom, n_cd, b_pn) {
            return $('#' + dom + ' span:nth-child(' + n_cd + ') i').css({"background-position": '0 ' + b_pn + 'px'});
        }
    });*/


    $.fn.RunTime = function (options) {

        var defaults = {sec: 0, min: 0, hour: 0, day: 1, month: 1, year: 2000};//默认值

        var targetDate_ar = options.targetDate.date.split(' ');
        var date_ar = targetDate_ar[0].split('-');
        var time_ar = targetDate_ar[1].split(':');

        var targetDate = {sec: time_ar[2], min: time_ar[1], hour: time_ar[0], day: date_ar[2], month: date_ar[1], year: date_ar[0]};
        var ReltargetDate = $.extend({}, defaults, targetDate);
        // console.log(ReltargetDate);
        var targetTime = new Date(ReltargetDate.month + '/' + ReltargetDate.day + '/' + ReltargetDate.year + ' ' + ReltargetDate.hour + ':' + ReltargetDate.min + ':' + ReltargetDate.sec + (ReltargetDate.utc ? ' UTC' : ''));

        var nowTime = new Date();

        var timestamp = Math.floor((nowTime.valueOf() - targetTime.valueOf()) / 1000);

        var secs = timestamp % 60;
        var mins = Math.floor(timestamp / 60) % 60;
        var hours = Math.floor(timestamp / 60 / 60) % 24;
        var mons = Math.floor(timestamp / 60 / 60 / 24 / 30) % 12;
        var days = Math.floor(timestamp / 60 / 60 / 24) % 365 % 30;
        var years = Math.floor(timestamp / 60 / 60 / 24 / 365);

        // console.log(years + '-' + mons + '-' + days + ' ' + hours + ':' + mins + ':' + secs);

        var time_arr = [secs, mins, hours, days, mons, years];

        return $(this).timeUp(time_arr, options.targetDate.unit);
    }


    //运行时间赋值
    $.fn.timeUp = function (time_arr, unit) {

        // console.log($(this));
        // console.log($(this).attr('id'))

        var timeUp_dom = $(this).attr('id');

        for (var i = 0; i < time_arr.length; i++) {
            $('#' + timeUp_dom).append('<span style="color:#FFF"><i></i>' + $.trim(unit[i]) + '</span>');
            $('#' + timeUp_dom + ' span:nth-child(' + i + ') i').animate({"background-position": '0 0'}, 'slow');
        }

        //运行时间初始化
        var count = time_arr.length;
        var New_time_arr = [];
        for (var key in time_arr) {
            var ph_val = (time_arr[key] == '') ? 0 : time_arr[key] * (-44);
            New_time_arr[key] = ph_val;
            $('#' + timeUp_dom + ' span:nth-child(' + count + ') i').css({"background-position": '0 ' + ph_val + 'px'});
            count--;
        }

        var sec = New_time_arr[0];
        var min = New_time_arr[1];
        var hour = New_time_arr[2];
        var day = New_time_arr[3];
        var mon = New_time_arr[4];
        var year = New_time_arr[5];

        setInterval(function () {
            sec = (sec == -2596) ? 0 : (sec - 44);
            t_item(timeUp_dom, 6, sec);
            if (sec == 0) {
                min = (min == -2596) ? 0 : (min - 44);
                t_item(timeUp_dom, 5, min);
                if (min == 0) {
                    hour = (hour == -1012) ? 0 : (hour - 44);
                    t_item(timeUp_dom, 4, hour);
                    if (hour == 0) {
                        day = (day == -1276) ? 0 : (day - 44);
                        t_item(timeUp_dom, 3, day);
                        if (day == 0) {
                            mon = (mon == -484) ? 0 : (mon - 44);
                            t_item(timeUp_dom, 2, mon);
                            if (mon == 0) {
                                year = year - 44;
                                t_item(timeUp_dom, 1, year);
                            }
                        }
                    }
                }
            }
        }, 1000);
    }

    function t_item(dom, n_cd, b_pn) {
        return $('#' + dom + ' span:nth-child(' + n_cd + ') i').css({"background-position": '0 ' + b_pn + 'px'});
    }

    /*//获取第几月份是今年的第几天
    $.fn.mon_ndays = function (mons) {
        var now = new Date();
        var monsDay = new Date(now.getFullYear(), mons, 1);
        var firstDay = new Date(now.getFullYear(), 0, 1);
        //计算当前时间与本年第一天的时差(返回一串数值，代表两个日期相差的毫秒数)
        var dateDiff = monsDay - firstDay;
        //一天的毫秒数
        var msPerDay = 1000 * 60 * 60 * 24;
        //计算天数
        var diffDays = Math.ceil(dateDiff / msPerDay);

        return diffDays;
    }
    console.log($.fn.mon_ndays(7));

    //js获取某年某月的天数
    $.fn.nMon_days = function() {
        var dayCount;
        var now = new Date();
        var monsDay = new Date(now.getFullYear(), now.getMonth() + 1, 0);
        dayCount = monsDay.getDate();
        return dayCount;
    }
    console.log($.fn.nMon_days());*/

})(jQuery);